import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    assess_token: '',  //初始化token
    id_token:'',
    userName:'',  //用户名称
    userId:''  //用户Id
  },
  mutations: {
    //存储token方法
    //设置token等于外部传递进来的值
    setAssessToken(state, assess_token) {
      state.assess_token = assess_token
      localStorage.assess_token = assess_token //同步存储token至localStorage
    },
    setIdToken(state, id_token) {
      state.id_token = id_token
      localStorage.id_token = id_token //同步存储token至localStorage
    },
    setUserName(state,userName){
      state.userName=userName
      localStorage.userName=userName
    },
    setUserId(state,userId){
      state.userId=userId
      localStorage.userId=userId
    }
  },
  getters: {
    //获取token方法
    //判断是否有token,如果没有重新赋值，返回给state的token
    getAssessToken(state) {
      if (!state.assess_token) {
        state.assess_token = localStorage.getItem('assess_token')
      }
      return state.assess_token
    },
    getIdToken(state) {
      if (!state.id_token) {
        state.id_token = localStorage.getItem('id_token')
      }
      return state.id_token
    },
    getUserName(state) {
      if (!state.userName) {
        state.userName = localStorage.getItem('userName')
      }
      return state.userName
    },
    getUserId(state) {
      if (!state.userId) {
        state.userId = localStorage.getItem('userId')
      }
      return state.userId
    }
  },
  actions: {

  }
})